# coding: utf-8
"""
   类型转换,将string类型转换为二进制或者将二进制转换为string类型的函数
"""
import binascii, json
import msgpack, hashlib
import base64
from qa.interface.app.parameters import encryption_android
from qa.interface.app.parameters import ia_token, ia_configure, ia_http
__author__ = 'jiaoshupeng'

class convert:
    def __init__(self, usersname, password):
        self.base = [str(x) for x in range(10)] + [ chr(x) for x in range(ord('A'),ord('A')+6)]
        # usersname = ia_configure.user   #主态登录用户
        # password = ia_configure.passwd      #主态用户密码
        self.guestUid = ia_configure.vice_uid
        print usersname, password
        self.login = ia_token.im_addHeader(1,usersname,hashlib.sha1(password).hexdigest(),1)#主态账号需要鉴权
        print self.login
        self.jianquan = self.login[0]
        self.header = self.login[1]
        self.pc = encryption_android.prpcrypt()

    def binary(self, value):  # 将string类型函数转换为二进制
        bin_16 = binascii.b2a_hex(value)
        bin_10 = int(bin_16, 16)
        bin_2 = bytearray(bin(bin_10))
        return bin_2

    def binInt(self,data):# 将long类型函数转换为二进制
        bin_10 = int(data, 16)
        bin_2 = self.bin(bin_10)
        return bin_2

    def restore(self, data):  # 将二进制转换为string类型
        bin_10 = int(data, 2)
        bin_16 = '%x' % bin_10
        result = binascii.a2b_hex(bin_16)
        return result

    def tokenBin(self,accessData):#将鉴权信息加密成二进制
        accData = base64.decodestring(accessData) #使用base64编码字符串
        accLen = len(accData)
        return accLen,accData

    def ArraysComb(self, heard='', heard1 = '', heard2='', heard3 = '', body=''):
        arr = []
        heardBin = heard
        if heard1 != '' and heard2 != '' and heard3 != '' and body !='':
            heard1Bin = int(bin(heard1).replace('0b',''),2)
            heard2Bin = bin(heard2).replace('0b','').zfill(32)
            heard3Bin = bin(heard3).replace('0b','').zfill(32)
            bodyBin = bytearray(body)
            arr.append(heardBin)
            arr.append(heard1Bin)
            arr.append(int(heard2Bin[0:8],2))
            arr.append(int(heard2Bin[8:16],2))
            arr.append(int(heard2Bin[16:24],2))
            arr.append(int(heard2Bin[24:32],2))
            arr.append(int(heard3Bin[0:8],2))
            arr.append(int(heard3Bin[8:16],2))
            arr.append(int(heard3Bin[16:24],2))
            arr.append(int(heard3Bin[24:32],2))
            for bin_2 in range(len(bodyBin)):
                arr.append(bodyBin[bin_2])
        else:
            arr.append(heardBin)
        return arr

    def getStart(self):
        token=self.jianquan['access_token']
        uid=self.jianquan['uid']
        en_text=self.pc.encrypt(token)
        ak_text=self.pc.padAK(en_text)
        res_token = self.tokenBin(ak_text)
        res_len = 4 + res_token[0]
        bin_data = self.ArraysComb(0b00011001, 3, res_len, uid, res_token[1])
        return bin_data

    def ping(self, num): # 客户端发送心跳包给服务端
        if num == 0:
            arr = []
            hearder = 0b00100010
            arr.append(hearder)
            version = int(bin(3).replace('0b',''),2)
            bodyLen = bin(2).replace('0b','').zfill(32)
            heardTim = bin(30).replace('0b','').zfill(16)
            arr.append(version)
            arr.append(int(bodyLen[0:8],2))
            arr.append(int(bodyLen[8:16],2))
            arr.append(int(bodyLen[16:24],2))
            arr.append(int(bodyLen[24:32],2))
            arr.append(int(heardTim[0:8],2))
            arr.append(int(heardTim[8:16],2))
            return arr
        else:
            pingData = self.ArraysComb(0b00100010)
            return pingData

    def sendWord(self, guestUid, msgContents):
        arrByte = []
        header = 0b00111001
        version = 3
        sessionmark = 0b00001000
        sessiontype = 2
        sessionId = guestUid
        msgLocalId = self.jianquan['last_login']
        msgType = 1
        fromId = self.jianquan['uid']
        msgContent = msgContents
        msgProfile = {'name': self.jianquan['name'], 'avatar': self.jianquan['avatar']}
        msgBody = {'type': msgType, 'from': fromId, 'contents': msgContent, 'profile': msgProfile}
        msgBodyPack = msgpack.packb(msgBody)
        msgLenth = 10 + len(msgBodyPack)
        versionBin = int(bin(version).replace('0b','').zfill(8),2)
        msgLenthBin = bin(msgLenth).replace('0b','').zfill(32)
        sessiontypeBin = int(bin(sessiontype).replace('0b','').zfill(8), 2)
        sessionIdBin = bin(sessionId).replace('0b','').zfill(32)
        msgLocalIdBin = bin(msgLocalId).replace('0b','').zfill(32)
        arrByte.append(header)
        arrByte.append(versionBin)
        arrByte.append(int(msgLenthBin[0:8],2))
        arrByte.append(int(msgLenthBin[8:16],2))
        arrByte.append(int(msgLenthBin[16:24],2))
        arrByte.append(int(msgLenthBin[24:32],2))
        arrByte.append(sessionmark)
        arrByte.append(sessiontypeBin)
        arrByte.append(int(sessionIdBin[0:8],2))
        arrByte.append(int(sessionIdBin[8:16],2))
        arrByte.append(int(sessionIdBin[16:24],2))
        arrByte.append(int(sessionIdBin[24:32],2))
        arrByte.append(int(msgLocalIdBin[0:8],2))
        arrByte.append(int(msgLocalIdBin[8:16],2))
        arrByte.append(int(msgLocalIdBin[16:24],2))
        arrByte.append(int(msgLocalIdBin[24:32],2))
        msgbodyBin = bytearray(msgBodyPack)
        for bin_2 in range(len(msgbodyBin)):
            arrByte.append(msgbodyBin[bin_2])
        return arrByte


    def getMsg(self): # 获取接口https://argo.blued.cn/users/6583813/basic 的返回信息
        interface = '/users/%s/basic'%self.guestUid
        phttp = ia_http.py_http()
        page=phttp.http_read(ia_configure.url_ym+interface,self.header,ia_configure.t_http,'')
        return page

    def readSend(self, sendData): #读取发送状态
        pass

if __name__ == '__main__':
    conClass = convert()
    conClass.sendWord()

